/* set more off */
/* capture log close */
/* clear */

use "./generated_data/7-data_for_regression_models.dta"

/* ORIGINAL MODEL 3, LEAVING OUT ONE PORTFOLIO AT THE TIME */
levelsof portfolio, local(levels)

foreach p of local levels {
    reg change_capital_share junior same_party i.pm##c.ws_cab_rescaled i.portfolio if portfolio != `p', vce(cluster budget_year)

    /* Store left-out portfolio and N obs for table */
    local portfolio_`p' : label portfolio `p'
    local N_`p' = e(N)

    /* Store coefficients for table */
    local b_position_`p' = _b[ws_cab_rescaled]
    local b_cowen_`p' = _b[2.pm]
    local b_kennedy_`p' = _b[3.pm]
    local b_cowenXpos_`p' = _b[2.pm#ws_cab_rescaled]
    local b_kennedyXpos_`p' = _b[3.pm#ws_cab_rescaled]

    /* Store standard errors for table */
    local se_position_`p' = _se[ws_cab_rescaled]
    local se_cowen_`p' = _se[2.pm]
    local se_kennedy_`p' = _se[3.pm]
    local se_cowenXpos_`p' = _se[2.pm#ws_cab_rescaled]
    local se_kennedyXpos_`p' = _se[3.pm#ws_cab_rescaled]
    
}

/* PRINT TABLE 3.3 IN APPENDIX */
foreach p of local levels {
    di "Portfolio:", "`portfolio_`p''"
    di "N obs:", `N_`p''
    di "Coefficients:", %12.2f `b_position_`p'', %12.2f `b_cowen_`p'', %12.2f `b_kennedy_`p'', %12.2f `b_cowenXpos_`p'', %12.2f `b_kennedyXpos_`p''
    di "Standard errors:",  %12.2f `se_position_`p'', %12.2f `se_cowen_`p'', %12.2f `se_kennedy_`p'', %12.2f `se_cowenXpos_`p'', %12.2f `se_kennedyXpos_`p''
    di "----------------------------------------------------------------------------------------------------"
}


/* Estimate regressions with unique ministers, averaging over DV */
/* ------------------------------------------------------------- */
/* Note: Budget data is by portfolio, not by department. As a result,
   the same minister can be assigned to more than one portfolio, which
   results in duplicate cases.  Therefore take the average change in
   the DV and drop duplicates. */
bysort yearMemberID: egen avg_change_capital_share = mean(change_capital_share)
bysort yearMemberID: egen avg_change_current_share = mean(change_current_share)
bysort yearMemberID: gen id = _n
bysort yearMemberID: gen member_count = _N

preserve
keep if id == 1

/* conditional on debt */
estimates clear
reg avg_change_capital_share junior same_party c.dpgChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store debt_capital

reg avg_change_current_share junior same_party c.dpgChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store debt_current


/* conditional on unemployment */    
reg avg_change_capital_share junior same_party c.urChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store ur_capital

reg avg_change_current_share junior same_party c.urChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store ur_current


/* conditional on PM */    
reg avg_change_capital_share junior same_party i.pm##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store pm_capital

reg avg_change_current_share junior same_party i.pm##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store pm_current

/* #delimit;
estout * using "./tables/appendix_table4.4-regression_output_all_models_unique_ministers.tex", style(tex)
cells("b(star fmt(2))" "se(par fmt(2))")
stats(r2 N, labels(R-squared "N. of cases") fmt(2 0))
starlevels(* 0.05)
label legend
varlabels(_cons Intercept)
replace;
#delimit cr */

/* PRINT TABLE 4.4 IN APPENDIX */
#delimit;
estout *,
cells("b(star fmt(2))" "se(par fmt(2))")
stats(r2 N, labels(R-squared "N. of cases") fmt(2 0))
starlevels(* 0.05)
label legend
varlabels(_cons Intercept)
replace;
#delimit cr

restore


/* Estimate regressions with cabinet ministers only (i.e., excluding junior ministers) */
/* -------------------------------------------------------------------------------------- */
preserve
drop if junior==1

/* conditional on debt */
estimates clear
reg change_capital_share same_party c.dpgChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store debt_capital

reg change_current_share same_party c.dpgChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store debt_current


/* conditional on unemployment */    
reg change_capital_share same_party c.urChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store ur_capital

reg change_current_share same_party c.urChange##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store ur_current


/* conditional on PM */    
reg change_capital_share same_party i.pm##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store pm_capital

reg change_current_share same_party i.pm##c.ws_cab_rescaled i.portfolio, vce(cluster budget_year)
estimates store pm_current

/* #delimit;
estout * using "./tables/appendix_table4.5-regression_output_cab_ministers_only.tex", style(tex)
cells("b(star fmt(2))" "se(par fmt(2))")
stats(r2 N, labels(R-squared "N. of cases") fmt(2 0))
starlevels(* 0.05)
label legend
varlabels(_cons Intercept)
replace;
#delimit cr */

/* PRINT TABLE 4.5 IN APPENDIX */
#delimit;
estout *,
cells("b(star fmt(2))" "se(par fmt(2))")
stats(r2 N, labels(R-squared "N. of cases") fmt(2 0))
starlevels(* 0.05)
label legend
varlabels(_cons Intercept)
replace;
#delimit cr

restore

